#include "include/system.h"
#define byte win_byte_override
#include "include/mysql.h"
#undef byte


// 引入全局变量
extern MysqlUser* mysql;

System::System() {

}
System::~System() {

}

// 系统主函数
void System::SystemMain() {
	// 初始化菜单
	int num;
	for (;;) {
		Menu();
		cout << "输出操作的序号：";
		cin >> num;
		if (num == 1) {
			SystemTripMain();
		}
		else if (num == 2) {
			SystemClientMain();
		}
		else if (num == 3) {
			SystemPlaneMain();
		}
		else if (num == 4) {
			SystemBookMain();
		}
		else if (num == 5) {
			break;
		}
		else {
			cout << "序号不在选择范围内！重新选择：" << endl;
		}
	}
}

void System::SystemTripMain() {
	// 初始化菜单
	
	Trip* trip = new Trip();
	int num;
	for (;;) {
		TripMenu();
		cout << "输出操作的序号：";
		cin >> num;
		if (num == 1) {
			cout << "依次输入航班号、飞机号、起飞城市、降落城市、机票价格、起飞时间、降落时间（时间格式：2003/09/10 13:44:40)" << endl;;
			unsigned int tripId, planeId;
			string takeOffCity, fallCity;
			double price;
			string takeOffTime,fallTime;
			cout << "输入航班号和飞机号：";
			// 读取航班号和飞机号
			cin >> tripId >> planeId;
			// 清空输入缓冲区
			cin.ignore();
			// 读取起飞城市和降落城市
			cout << "请输入起飞城市：";
			getline(cin, takeOffCity);
			cout << "请输入降落城市：";
			getline(cin, fallCity);
			// 读取机票价格
			cout << "请输入机票价格：";
			cin >> price;
			// 清空输入缓冲区
			cin.ignore();
			// 读取起飞时间和降落时间
			cout << "请输入起飞时间（格式：2003/09/10 13:44:40）：";
			getline(cin, takeOffTime);
			cout << "请输入降落时间（格式：2003/09/10 13:44:40）：";
			getline(cin, fallTime);
			cout << endl;
			if (trip->AddTrip(tripId, planeId, takeOffCity, fallCity, price, takeOffTime, fallTime)) {
				cout << "增加成功" << endl;
			}
			else {
				cout << "发生错误" << endl;
			}
		}
		else if (num == 2) {
			cout << "输入航班号：";
			unsigned int tripId;
			cin >> tripId;
			if (trip->DeleteTrip(tripId)) {
				cout << "删除成功" << endl;
			}
			else {
				cout << "发生错误" << endl;
			}
		}
		else if (num == 3) {
			cout << "输入航班号：";
			unsigned int tripId;
			cin >> tripId;
			cout << "分别输入key和value：";
			string key, value;
			getline(cin, key);
			getline(cin, value);
			cin.ignore();
			map<string, string> entry;
			entry[key] = value;
			if (trip->ModifyTrip(tripId,entry)) {
				cout << "修改成功" << endl;
			}
			else {
				cout << "发生错误" << endl;
			}
		}
		else if (num == 4) {
			cout << "输入航班号：";
			unsigned int tripId;
			cin >> tripId;
			trip->SearchTrip(tripId);
			// 打印结果
			cout << "查询结果：" << endl;
			cout << trip->getTripId() << ", " << trip->getPlaneId() << ", " << trip->getTakeOffCity() << ", " << trip->getFallCity() << ", " << trip->getPrice() << ", " << trip->getTakeOffTime() << ", " << trip->getFallTime() << endl;
		}
		else if (num == 5) {
			vector<Trip> trips = trip->SearchAllTrip();
			cout << "查询结果： " << endl;
			for (int i = 0; i < trips.size(); i++) {
				cout << trips[i].getTripId() << ", " << trips[i].getPlaneId() << ", " << trips[i].getTakeOffCity() << ", " << trips[i].getFallCity() << ", " << trips[i].getPrice() << ", " << trips[i].getTakeOffTime() << ", " << trips[i].getFallTime() << endl;
			}
		}
		else if (num == 6) {
			break;
		}
		else {
			cout << "序号不在选择范围内！重新选择：" << endl;
		}
	}
}
void System::SystemClientMain() {
	// 初始化菜单
	
	Client* client = new Client();
	int num;
	for (;;) {
		ClientMenu();
		cout << "输出操作的序号：";
		cin >> num;
		if (num == 1) {
			cout << "输入旅客号：";
			int64_t Id;
			cin >> Id;
			if (client->DeleteClient(Id)) {
				cout << "删除成功" << endl;
			}
			else {
				cout << "发生错误" << endl;
			}
		}
		else if (num == 2) {
			cout << "输入旅客号：";
			int64_t tripId;
			cin >> tripId;
			cout << "分别输入key和value：";
			string key, value;
			getline(cin, key);
			getline(cin, value);
			cin.ignore();
			map<string, string> entry;
			entry[key] = value;
			if (client->ModifyClient(tripId, entry)) {
				cout << "修改成功" << endl;
			}
			else {
				cout << "发生错误" << endl;
			}
		}
		else if (num == 3) {
			cout << "输入旅客号：";
			int64_t tripId;
			cin >> tripId;
			client->SearchClient(tripId);
			// 打印结果
			cout << "查询结果：" << endl;
			cout << client->getClientId() << ", " << client->getClientName() << ", " << client->getClientPhone() << ", " << client->getPassWord() << endl;
		}
		else if (num == 4) {
			vector<Client> trips = client->SearchAllClient();
			cout << "查询结果： " << endl;
			for (int i = 0; i < trips.size(); i++) {
				cout << trips[i].getClientId() << ", " << trips[i].getClientName() << ", " << trips[i].getClientPhone() << ", " << trips[i].getPassWord() << endl;
			}
		}
		else if (num == 5) {
			break;
		}
		else {
			cout << "序号不在选择范围内！重新选择：" << endl;
		}
	}
}
void System::SystemPlaneMain() {
	
	Plane* plane = new Plane();
	int num;
	for (;;) {
		// 初始化菜单
		PlaneMenu();
		cout << "输出操作的序号：";
		cin >> num;
		if (num == 1) {
			cout << "依次输入航班号、飞机型号、飞机座位数" << endl;
			unsigned int planeId; string planeModel; unsigned int seatSize;
			cout << "输入航班号、飞机座位数：";
			cin >> planeId>> seatSize;
			cin.ignore();
			cout << "输入航班号：";
			getline(cin,planeModel);
			if (plane->AddPlane(planeId, planeModel, seatSize)) {
				cout << "增加成功" << endl;
			}
			else {
				cout << "发生错误" << endl;
			}
		}
		else if (num == 2) {
			cout << "输入飞机号：";
			unsigned int tripId;
			cin >> tripId;
			cout << "分别输入key和value：";
			string key, value;
			getline(cin, key);
			getline(cin, value);
			cin.ignore();
			map<string, string> entry;
			entry[key] = value;
			if (plane->ModifyPlane(tripId, entry)) {
				cout << "修改成功" << endl;
			}
			else {
				cout << "发生错误" << endl;
			}
		}
		else if (num == 3) {
			cout << "输入飞机号：";
			unsigned int Id;
			cin >> Id;
			if (plane->DeletePlane(Id)) {
				cout << "删除成功" << endl;
			}
			else {
				cout << "发生错误" << endl;
			}
		}
		else if (num == 4) {
			cout << "输入飞机号：";
			unsigned int tripId;
			cin >> tripId;
			plane->SearchPlane(tripId);
			// 打印结果
			cout << "查询结果：" << endl;
			cout << plane->getPlaneId() << ", " << plane->getPlaneModel() << ", " << plane->getSeatSize() << endl;
		}
		else if (num == 5) {
			vector<Plane> trips = plane->SearchAllPlane();
			cout << "查询结果： " << endl;
			for (int i = 0; i < trips.size(); i++) {
				cout << trips[i].getPlaneId() << ", " << trips[i].getPlaneModel() << ", " << trips[i].getSeatSize() << endl;
			}
		}
		else if (num == 6) {
			break;
		}
		else {
			cout << "序号不在选择范围内！重新选择：" << endl;
		}
	}
}
void System::SystemBookMain() {
	
	Book* book = new Book();
	int num;
	for (;;) {
		// 初始化菜单
		BookMenu();
		cout << "输出操作的序号：";
		cin >> num;
		if (num == 1) {
			cout << "输入旅客号：";
			int64_t tripId;
			cin >> tripId;
			book->SearchBook(tripId);
			// 打印结果
			cout << "查询结果：" << endl;
			cout << book->getBookingId() << ", " << book->getClientId() << ", " << book->getTripId() << ", " << book->getParseBookTime() << ", " << book->getParseRefundTime() << endl;;
		}
		else if (num == 2) {
			vector<Book> trips = book->SearchAllBook();
			cout << "查询结果： " << endl;
			for (int i = 0; i < trips.size(); i++) {
				cout << trips[i].getBookingId() << ", " << trips[i].getClientId() << ", " << trips[i].getTripId() << ", " << trips[i].getParseBookTime() << ", " << trips[i].getParseRefundTime() << endl;;
			}
		}
		else if (num == 3) {
			break;
		}
		else {
			cout << "序号不在选择范围内！重新选择：" << endl;
		}
	}
}

void System::Menu() {
	cout << "选择操作的对象" << endl;
	cout << "1.航班信息" << endl;
	cout << "2.旅客信息" << endl;
	cout << "3.飞机信息" << endl;
	cout << "4.订单信息" << endl;
	cout << "5.退出" << endl;
}

void System::TripMenu() {
	cout << "选择操作的对象" << endl;
	cout << "1.增加航班" << endl;
	cout << "2.删除航班" << endl;
	cout << "3.修改航班" << endl;
	cout << "4.查询航班" << endl;
	cout << "5.查询所有航班" << endl;
	cout << "6.退出" << endl;
}
void System::ClientMenu() {
	cout << "选择操作的对象" << endl;
	cout << "1.删除旅客" << endl;
	cout << "2.修改旅客" << endl;
	cout << "3.查询旅客" << endl;
	cout << "4.查询所有旅客" << endl;
	cout << "5.退出" << endl;

}
void System::PlaneMenu() {
	cout << "选择操作的对象" << endl;
	cout << "1.增加飞机" << endl;
	cout << "2.修改飞机" << endl;
	cout << "3.删除飞机" << endl;
	cout << "4.查询飞机" << endl;
	cout << "5.查询所有飞机" << endl;
	cout << "6.退出" << endl;
}
void System::BookMenu() {
	cout << "选择操作的对象" << endl;
	cout << "1.查询用户订单" << endl;
	cout << "2.查询所有订单" << endl;
	cout << "3.退出" << endl;
}